<?
/**********************
* ÁLTALÁNOS FÜGGVÉNYEK*
***********************/

     function refresh(){ //without _GET
		redirect($_SERVER['PHP_SELF']);
	}
     function refresh_get(){ //with _GET
		redirect($_SERVER['PHP_SELF'].'?'.$_SERVER["QUERY_STRING"]);
	}
     function refresh_with_get(){refresh_get();} //alias
     function redirect($s){
		header("Location: $s");
		die;
	}
	
     function error($e){
		$error="Hiba: ".$e->getMessage()." - a /".getRelativePath(str_replace('\\',"/",$e->getFile()), str_replace('\\',"/",$_SERVER["DOCUMENT_ROOT"].'petshop/'))." fájlban, a ".$e->getLine()." sorban. Trace: ".$e->getTraceAsString()."\n";
		file_put_contents("error.log", $error, FILE_APPEND);
		redirect("../gameerror.php?code=exception");
	}
     function debug_error($e){
		echo "Hiba: ".$e->getMessage()." - a /".$e->getFile()." fájlban, a ".$e->getLine()." sorban. Trace: ".$e->getTraceAsString()."\n";
		exit;
	}
     function ex_throw($e){
		throw $e;
	}
	
     function str_str($haystack, $needle, $before_needle=false){
		if($before_needle==false){
			return strstr($haystack, $needle);
		}
		else{
			$pos = strpos($haystack, $needle);
			return substr($haystack, 0, $pos);
		}
	}
	
     function getRelativePath( $path, $compareTo ) {
        if ( substr( $path, -1 ) == '/' ) {
            $path = substr( $path, 0, -1 );
		}
        if ( substr( $path, 0, 1 ) == '/' ) {
            $path = substr( $path, 1 );
        }
        if ( substr( $compareTo, -1 ) == '/' ) {
            $compareTo = substr( $compareTo, 0, -1 );
        }
        if ( substr( $compareTo, 0, 1 ) == '/' ) {
            $compareTo = substr( $compareTo, 1 );
        }
        // simple case: $compareTo is in $path
        if ( strpos( $path, $compareTo ) === 0 ) {
            $offset = strlen( $compareTo ) + 1;
            return substr( $path, $offset );
        }
        $relative  = array(  );
        $pathParts = explode( '/', $path );
        $compareToParts = explode( '/', $compareTo );
        foreach( $compareToParts as $index => $part ) {
            if ( isset( $pathParts[$index] ) && $pathParts[$index] == $part ) {
                continue;
            }
            $relative[] = '..';
        }
        foreach( $pathParts as $index => $part ) {
            if ( isset( $compareToParts[$index] ) && $compareToParts[$index] == $part ) {
                continue;
            }
            $relative[] = $part;
        }
        return implode( '/', $relative );
    }
	
     function strtoproper($str)
    {
        $noUp = array('és');
        $str = trim($str);
        $str = strtoupper($str[0]) . strtolower(substr($str, 1));
        for($i=1; $i<strlen($str)-1; ++$i) {
            if($str[$i]==' ') {
                for($j=$i+1; $j<strlen($str) && $str[$j]!=' '; ++$j); //find next space
                $size = $j-$i-1;
                $shortWord = false;
                if($size<=3) {
                    $theWord = substr($str,$i+1,$size);
                    for($j=0; $j<count($noUp) && !$shortWord; ++$j)
                        if($theWord==$noUp[$j])
                            $shortWord = true;
                }
                if( !$shortWord )
                    $str = substr($str, 0, $i+1) . strtoupper($str[$i+1]) . substr($str, $i+2);
            }  
            $i+=$size;
        }
        return $str;
    }
     function test($s){
		echo $s;
		return $s;
	}
	
function mb_str_split($string, $split_length = 1){
           
        mb_internal_encoding('UTF-8');
        mb_regex_encoding('UTF-8'); 
       
        $split_length = ($split_length <= 0) ? 1 : $split_length;
       
        $mb_strlen = mb_strlen($string, 'utf-8');
       
        $array = array();
               
        for($i = 0; $i < $mb_strlen; $i += $split_length){
       
            $array[] = mb_substr($string, $i, $split_length);
        }

        return $array;
   
    }
	

function get_time_distance($timestr){
	$str;
	$dist=abs(time()-$timestr);
	$str['week'] = $str['day'] = $str['hour'] = $str['minute'] = 0;
	if($dist>=604800){
		$het = floor($dist/604800);
		$str['week'] = $het;
		$dist -= $het*604800;
	}
	if($dist>=86400){
		$nap = floor($dist/86400);
		$str['day'] = $nap;
		$dist -= $nap*86400;
	}
	if($dist>=3600){
		$ora = floor($dist/3600);
		$str['hour'] = $ora;
		$dist -= $ora*3600;
	}
	if($dist>=60){
		$perc = floor($dist/60);
		$str['minute'] = $perc;
		$dist -= $perc*60;
	}
	$str['second']=$dist;
	return $str;	
}

function get_first_letter($str){
	switch(strtolower(substr($str, 0, 2))){
		case 'gy':
		case 'dz':
		case 'cs':
		case 'sz':
		case 'ny':
		case 'ty':
			return substr($str, 0, 2);
		default:
			return substr($str, 0, 1);
	}
}
function if_data($s){
	if(empty($s)){
		return '{no_data}';
	}
	else{
		return $s;
	}
}
function add_http($s){ // hozzáadja egy webcímhez a "http://" előtegot ha nincs még ott és a szöveg nem üres
	if(!empty($s)){ //ha a string nem üres
		if(substr($s,0,4)!="http"){ //...és nincs http...
			$s = "http://".$s; //...akkor benyomjuk elé
		}
	}
	return $s;
}
function crop_text($text, $length){
	if(mb_strlen($text)<=$length){
		return $text;
	}
	else{
		$dotpos = mb_strrpos(mb_substr($text, 0, $length), '.');
		if($dotpos==false){ // ha nincs pont az első <length> karakterben
			return mb_substr($text, 0, $length).'...'; // akk simán vágjuk
		}
		return mb_substr($text, 0, $dotpos+1).'..';
	}
}

function start_transaction(){
	return mysql_query("START TRANSACTION");
}
function end_transaction(){
	return mysql_query("COMMIT");
}

function lock_table($t, $mode = "WRITE"){
	return mysql_query("LOCK TABLES ".$t." ".$mode);
}
function unlock_tables(){
	return mysql_query("UNLOCK TABLES");
}





?>
